package com.genesis.web.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.genesis.mall.common.Result;
import com.genesis.mall.dto.PageDto;
import com.genesis.mall.entity.UmsResource;
import com.genesis.mall.entity.UmsRoleMenuRelation;
import com.genesis.mall.entity.UmsRoleResourceRelation;
import com.genesis.mall.service.IUmsResourceService;
import com.genesis.mall.service.IUmsRoleResourceRelationService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 后台资源表 前端控制器
 * </p>
 *
 * @author author
 * @since 2022-08-01
 */
@RestController
@RequestMapping("/resource")
public class UmsResourceController {
    @Autowired
    private IUmsResourceService resourceService;

    @Autowired
    private IUmsRoleResourceRelationService roleResourceRelationService;

    /**
     * 查询所有的资源
     * @return
     */
    @GetMapping("/listAll")
    public Result<List<UmsResource>> listAll(){
        return Result.success(resourceService.list());
    }
    /**
     * 分页查询资源列表
     */
    @GetMapping("/list")
    public Result<PageDto<UmsResource>> list(
            PageDto<UmsResource> pageDto,
            String categoryId,
            String nameKeyword,
            String urlKeyword){
        Page<UmsResource> page = new Page<>(pageDto.getPageNum(), pageDto.getPageSize());
        LambdaQueryWrapper<UmsResource> lqw = new LambdaQueryWrapper<>();
        lqw.like(StringUtils.isNotEmpty(nameKeyword),UmsResource::getName,nameKeyword)
                .like(StringUtils.isNotEmpty(urlKeyword),UmsResource::getUrl,urlKeyword)
                .eq(StringUtils.isNotEmpty(categoryId),UmsResource::getCategoryId,categoryId);
        resourceService.page(page,lqw);
        //封装返回值
        pageDto.setTotalPage((int)page.getPages());
        pageDto.setTotal((int)page.getTotal());
        pageDto.setList(page.getRecords());
        return Result.success(pageDto);
    }
    /**
     * 添加资源
     */
    @PostMapping("/create")
    public Result<String> create(@RequestBody UmsResource umsResource){
        umsResource.setCreateTime(LocalDateTime.now());
        resourceService.save(umsResource);
        return Result.success();
    }

    /**
     * 根据id删除后台资源
     */
    @PostMapping("/delete/{id}")
    public Result<String> delete(@PathVariable Long id){
        LambdaQueryWrapper<UmsRoleResourceRelation> lqw =new LambdaQueryWrapper<>();
        lqw.eq(UmsRoleResourceRelation::getResourceId,id);
        List<UmsRoleResourceRelation> relations = roleResourceRelationService.list(lqw);
        if (relations!=null){
            return Result.error("有关联角色,无法删除");
        }
        resourceService.removeById(id);
        return Result.success();
    }

    /**
     * 根据id获取资源详情
     */
    @GetMapping("/{id}")
    public Result<UmsResource> findById(@PathVariable Long id){
        UmsResource umsResource = resourceService.getById(id);
        return Result.success(umsResource);
    }

    /**
     * 修改后台资源
     */
    @PostMapping("/update/{id}")
    public Result<String> update(@RequestBody UmsResource umsResource){
        resourceService.updateById(umsResource);
        return Result.success();
    }
}
